#import "AvdSdkPlugin.h"
#import "AVDFlutterWrapper.h"
#import "RTCVideoViewFactory.h"

@implementation AvdSdkPlugin

-(void)dealloc{
    NSLog(@"AvdSdkPlugin delloc ♻️");
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    dispatch_async(dispatch_get_main_queue(), ^{
        FlutterMethodChannel* channel = [FlutterMethodChannel
            methodChannelWithName:@"avd_sdk_plugin"
                  binaryMessenger:[registrar messenger]];
        AvdSdkPlugin* instance = [[AvdSdkPlugin alloc] init];
        [registrar addMethodCallDelegate:instance channel:channel];
        RTCVideoViewFactory *factory = [[RTCVideoViewFactory alloc] initWithMessenger:registrar.messenger];
        [registrar registerViewFactory:factory withId:@"avd_video_render"];
        [[AVDFlutterWrapper sharedWrapper] setFlutterChannel:channel];
        [[AVDFlutterWrapper sharedWrapper] setVideoViewFactory:factory];
        
    });
    
//  FlutterMethodChannel* channel = [FlutterMethodChannel
//      methodChannelWithName:@"avd_sdk_plugin"
//            binaryMessenger:[registrar messenger]];
//  AvdSdkPlugin* instance = [[AvdSdkPlugin alloc] init];
//  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[AVDFlutterWrapper sharedWrapper] setFlutterChannel:nil];
        [[AVDFlutterWrapper sharedWrapper] setVideoViewFactory:nil];
        
    });
    
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    [[AVDFlutterWrapper sharedWrapper] handleMethodCall:call result:result];
}

//- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
//  if ([@"getPlatformVersion" isEqualToString:call.method]) {
//    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
//  } else {
//    result(FlutterMethodNotImplemented);
//  }
//}

@end
